package ph.loong.deliveryman.fragment.account

import android.content.Intent
import android.text.TextUtils
import com.administrator.kotlindemo1.network.RetrofitManager
import com.administrator.kotlindemo1.utils.showToast
import com.bumptech.glide.Glide
import com.hazz.kotlinmvp.rx.scheduler.SchedulerUtils
import deliveryman.loong.ph.deliveryman.R
import kotlinx.android.synthetic.main.fragment_pwd_login.*
import ph.loong.deliveryman.base.BaseFragment
import ph.loong.deliveryman.constant.Constants
import ph.loong.deliveryman.ui.account.ForgetPwdActivity
import ph.loong.deliveryman.utils.SPUtil
import ph.loong.deliveryman.utils.SecurityUtil

/**
 * Author jc.lu.
 * Dates 2018/2/8.
 * Email Tbsupland@163.com.
 * desc
 */
class PwdLoginFragment : BaseFragment() {

    private var tokenStr: String? = null

    override fun getLayoutId(): Int = R.layout.fragment_pwd_login

    override fun initView() {
//        verifiyRequest()
        //登录button
        login_btn.setOnClickListener({
            loginRequest()
        })
        //重新获取验证码
        verifiy_img.setOnClickListener({ verifiyRequest() })
        forget_pwd_text.setOnClickListener({
            val intent = Intent(context, ForgetPwdActivity::class.java)
            startActivity(intent)
        })
    }

    /**
     * 登录
     */
    private fun loginRequest() {
        val phoneEdit = phone_edit.text
        val pwdEdit = pwd_edit.text
        var verifiyEdit = verifiy_edit.text
        if (TextUtils.isEmpty(phoneEdit) || TextUtils.isEmpty(pwdEdit) || TextUtils.isEmpty(verifiyEdit)) {
            showToast("账户名密码验证码不能为空")
            return
        }
        showDialog()
        RetrofitManager.service.deliveryLogin(tokenStr!!, verifiyEdit.toString(), SecurityUtil.MD5(pwdEdit.toString()), "1", phoneEdit.toString(), "86")
                .compose(SchedulerUtils.ioToMain())
                .subscribe(
                        { bean ->
                            dissmissDialog()
                            showToast(bean.toString())
                            verifiyRequest()
                        }
                        , {
                    dissmissDialog()
                    showToast("error")
                })

    }

    /**
     * 验证码接口
     */
    private fun verifiyRequest() {
        showDialog()
        RetrofitManager.service.getToken()
                .compose(SchedulerUtils.ioToMain())
                .subscribe({ bean ->
                    if (bean.code == 1) {
                        dissmissDialog()
                        tokenStr = bean.data.token
                        SPUtil.put("token", tokenStr)
                        var replace = tokenStr?.replace("+", "%2b")
                        replace = Constants.BASE_URL + "/waimai/openservice/getCodeImage?token=" + replace
                        Glide.with(context).load(replace).into(verifiy_img)
                    }
                },
                        { Throwable ->
                            dissmissDialog()
                            showToast("error")
                        })
    }

    override fun lazyLoad() {

    }
}